package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.bean.Category;
import com.chen.bean.Setmeal;
import com.chen.bean.SetmealDish;
import com.chen.common.R;
import com.chen.dto.SetmealDto;
import com.chen.exception.CustomException;
import com.chen.mapper.SetmealDao;
import com.chen.mapper.SetmealDishDao;
import com.chen.service.CategoryService;
import com.chen.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal>implements SetmealService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishDao setmealDishDao;

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R<Page<SetmealDto>> setmealPage(Integer page, Integer pageSize, String name) {
        Page<Setmeal> oldPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        Page<Setmeal> setmealPage = this.page(oldPage, wrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<SetmealDto> setmealDtoList = new ArrayList<>();

        List<Setmeal> setmealList = setmealPage.getRecords();
        setmealList.forEach(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);

            Category category = categoryService.getById(setmeal.getCategoryId());

            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        });
        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @Override
    public R<String> addSetmeal(SetmealDto setmealDto) {
        //判断套餐名字是否重复
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getName,setmealDto.getName());
        int count = this.count(wrapper);
        if (count>0){
            throw new CustomException("套餐名称"+setmealDto.getName()+"已存在");
        }
        //存储套餐
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishDao.insert(setmealDish);
        });
        return R.success("新增成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @Override
    public R<String> deleteSetmael(Long[] ids) {
        if (ids==null||ids.length==0)
            throw new CustomException("请选择后在删除");
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        //删除套餐
        this.remove(wrapper);
        //删除套餐中的菜品
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishDao.delete(setmealDishWrapper);

        return R.success("删除成功");
    }

    /**
     * 修改套餐状态
     * @param status
     * @param ids
     * @return
     */
    @Override
    public R<String> updateStatus(Integer status, Long[] ids) {
        if (ids == null || ids.length == 0)
            throw new CustomException("请选择后在修改");

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        this.update(setmeal, wrapper);

        return R.success("修改成功");
    }

    /**
     * 回显要修改的数据
     * @param id
     * @return
     */
    @Override
    public R<SetmealDto> echoSetmeal(Long id) {
        //创建setmealdto对象 用来封装数据
        SetmealDto setmealDto = new SetmealDto();
        //根据id查询setmeal
        Setmeal setmeal = this.getById(id);
        //根据setmealid 查询所属的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishDao.selectList(wrapper);
        //将数据封装
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @Override
    public R<String> updateSetmeal(SetmealDto setmealDto) {
        //判断套餐名字是否重复
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getName,setmealDto.getName());
        wrapper.ne(Setmeal::getId,setmealDto.getId());
        int count = this.count(wrapper);
        if (count>0){
            throw new CustomException("套餐名称"+setmealDto.getName()+"已存在");
        }
        //修改套餐
        this.updateById(setmealDto);
        //修改套餐菜品
        //1，先删除原有菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishDao.delete(queryWrapper);
        //2，重新添加菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishDao.insert(setmealDish);
        });
        return R.success("修改成功");

    }


}
